﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Web.Security;
using System.Text;
using System.Collections.Specialized;
using Utils;


public partial class account_invite : FBasePage
{
    // Fields
  
    public string strCount = "";
    public string strCurrency = "";
    public string strInvitecredit = "";
    public string strMoney = "";
    public string strRealName = "";
    public string strTeamID = "";
    public string strTeamImg = "";
    public string strTeamTitle = "";
    public string strUserID = "";
    public string strWebName = "";
    public string strWebSite = "";

    // Methods
    private void GetSystemInfo()
    {
        string str = "";
        DataSet list = new Maticsoft.BLL.System().GetList("");
        if (((list != null) && (list.Tables.Count > 0)) && (list.Tables[0].Rows.Count > 0))
        {
            str = list.Tables[0].Rows[0]["Currency"].ToString();
            if ((list.Tables[0].Rows[0]["abbreviation"] == null) || (list.Tables[0].Rows[0]["abbreviation"].ToString() == ""))
            {
                this.strWebName = "艾尚团购";
            }
            else
            {
                this.strWebName = list.Tables[0].Rows[0]["abbreviation"].ToString();
            }
            this.strInvitecredit = list.Tables[0].Rows[0]["Invitecredit"].ToString();
        }
        string name = this.Page.User.Identity.Name;
        Maticsoft.BLL.UserInfo info = new Maticsoft.BLL.UserInfo();
        string strWhere = "";
        strWhere = string.Format(" Username='{0}'", name);
        DataSet set2 = info.GetList(strWhere);
        if (((set2 != null) && (set2.Tables.Count > 0)) && (set2.Tables[0].Rows.Count > 0))
        {
            this.strUserID = set2.Tables[0].Rows[0]["id"].ToString();
            this.strRealName = set2.Tables[0].Rows[0]["Realname"].ToString();
        }
        this.strCount = new Maticsoft.BLL.Invite().GetCount("Count(*)", " User_id=" + this.strUserID);
        this.strMoney = (int.Parse(this.strCount) * int.Parse(float.Parse(this.strInvitecredit).ToString())).ToString();
    }

    private void GetTeamNow()
    {
        if (base.CurrentTeam != null)
        {
            this.strTeamID = base.CurrentTeam.Id.ToString();
            this.strTeamImg = base.CurrentTeam.Image.ToString();
            this.strTeamTitle = base.CurrentTeam.Title.ToString();
        }
        else
        {
            this.pimg.Visible = false;
        }
    }

    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);
        if (!this.Page.IsPostBack)
        {
            if (!(this.Page.User.Identity.IsAuthenticated && !(this.Page.User.Identity.Name == "")))
            {
                base.Response.Redirect("Login.aspx");
            }
            this.GetSystemInfo();
            this.GetTeamNow();
            this.strWebSite = base.Request.Url.AbsoluteUri;
            this.strWebSite = this.strWebSite.Substring(7);
            this.strWebSite = "http://" + this.strWebSite.Substring(0, this.strWebSite.IndexOf('/'));
        }
        if (base.Request.HttpMethod == "POST")
        {
            this.sentEmail();
        }
    }

    private void sentEmail()
    {
        string str = "";
        string str2 = "";
        string name = this.Page.User.Identity.Name;
        Maticsoft.BLL.UserInfo info = new Maticsoft.BLL.UserInfo();
        string strWhere = "";
        strWhere = string.Format(" Username='{0}'", name);
        DataSet set = info.GetList(strWhere);
        if (((set != null) && (set.Tables.Count > 0)) && (set.Tables[0].Rows.Count > 0))
        {
            this.strUserID = set.Tables[0].Rows[0]["id"].ToString();
            this.strRealName = set.Tables[0].Rows[0]["Realname"].ToString();
        }
        str2 = base.Request.Url.AbsoluteUri.Substring(7);
        str2 = "http://" + str2.Substring(0, str2.IndexOf('/'));
        NameValueCollection values = new NameValueCollection();
        values.Add("name", this.strRealName);
        values.Add("sitename", str);
        values.Add("wwwprefix", str2);
        values.Add("content", base.Request.Form["invitation_content"].ToString());
        values.Add("user_id", this.strUserID);
        string str5 = WebSiteHelper.LoadTemplate("~/template/mail_invite.HTML", values);
        string subject = "您的好友[" + name + "]邀请您注册" + base.ASSystemArr["abbreviation"];
        string body = str5;
        string[] strArray = base.Request.Form["recipients"].ToString().Replace("\r\n", "|").Split(new char[] { '|' });
        List<string> toMails = new List<string>();
        for (int i = 0; i < strArray.Length; i++)
        {
            toMails.Add(strArray[i]);
        }
        string error = string.Empty;
        if (base.SendMail(toMails, subject, body, out error))
        {
            base.SetSuccess("邮件发送成功！");
            base.Response.Redirect("invite.aspx");
        }
        else
        {
            this.SetError("邮件发送失败！请检查SMTP设置");
            base.Response.Redirect("invite.aspx");
        }
    }

}